#define _MAIN_CPP_

#include <iostream>
#include "SMATModel.hh"
#include "display.hh"
#include "utils.hh"
#include <string.h>

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>

#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>

#include "facedir.h"

#include "other_functions.h"

#include "view.h"
#include "model.h"
#include "createmodel.h"
#include "cvfundam2.h"

#include "options.h"

char window[4][100] = { "Camara Left", "Camara Right"};
char Model_SMAT[2][100] = { "Model+SMAT Left", "Model+SMAT Right"};

int main(int argc, char *argv[])
{
    if (!leer_opciones(argc, argv))
	return 0;

    //srand( (unsigned)time( NULL ) );
    srand(1);  //siempre la misma secuencia aleatoria. Debug

    strcpy(window[2], Model_SMAT[0]);
    strcpy(window[3], Model_SMAT[1]);
    std::string buffer;
    CFacedir facedir;

    facedir.iniframe = ini_opt("iniframe");
    facedir.endframe = ini_opt("endframe");

   
    facedir.wait = ini_opt("wait");
    facedir.graficar = ini_opt("graficar");
    facedir.save_video = ini_opt("ov");
    facedir.save_pose = ini_opt("op");
    facedir.reader.init_params(ini_opts("v"), ini_opts("format"), facedir.iniframe);

    if (ini_opt("r"))
    {
	FILE *f;
	std::string buffer = facedir.reader.get_pathbasename()+ 
	    format(".dat.%d", facedir.iniframe);
	f=fopen(buffer.c_str(), "r");
	if (!f)
	    buffer = facedir.reader.get_pathbasename()+ ".dat";
	else
	    fclose(f);
	printf("Fichero de modelo 3D: %s\n", buffer.c_str());
	facedir.filemodel = buffer;
    }
    else
	facedir.filemodel = "";
    
    facedir.basefilename = facedir.reader.get_pathbasename();

    facedir.filemodelsmat = ini_opts("m");

    facedir.main();
}

struct option2 mainopts2[]= {
    {{"v", 1, NULL, OPT_VAL_IS_CHAR}, 0, "", 
     "Video to process"},
    {{"m", 1, NULL, OPT_VAL_IS_CHAR}, 0, "model_test_31.xml",
     "The file containing the SMAT definition."},
    {{"format", 0, NULL, OPT_VAL_IS_CHAR}, 0, "", 
     "format = [raw | ra2 | avi]: Tells the format of the video"},
    {{"op", 0, NULL, true}, false, "", 
     "Saves the pose values after each frame. The file name will be <FILE>_pose_results.xml"},
    {{"ov", 0, NULL, true}, false, "", 
     "Saves a video of the results. They will be stored in video_results_SMAT.avi"
     " and video_results_Facedir.avi"}, 
    {{"lines", 1, NULL, OPT_VAL_IS_INT}, 4, "", 
     "tIndicates de number of lines to draw which connects from each node"},
    {{"r", 0, NULL, true}, false, "", 
     "Read Model: Does not autocreate the Model. Reads if from a file"},
    {{"wait", 1, NULL, OPT_VAL_IS_INT}, 0, "", 
     "tIndicates de parameter for the cvWaitKey(n)"},
    {{"cam", 0, NULL, true}, false, "", 
     "Capture de video from the cameras"},

    {{"groundtruth", 0, NULL, true}, false, "", 
     "procesado del groundtruth"},
    {{"dat", 1, NULL, OPT_VAL_IS_INT}, 2, "", 
     "indica el numero N del archivo .daN a leer como datos del modelo"},
    {{"data", 1, NULL, OPT_VAL_IS_CHAR}, 0, "", 
     "..."},
    {{"graficar", 0, NULL, true}, false, "", 
     "Muestra le proceso de matching 3D"},

    {{"options", 0, NULL, true}, false, "", 
     "Imprime las opciones de configuracion del programa"},
    {{"h", 0, NULL, true}, false, "", 
     ""},
    {{"inivect", 1, NULL, OPT_VAL_IS_CHAR}, 0, "",
     "Vector inicial de direcicón"},

    {{"iniframe", 1, NULL, OPT_VAL_IS_INT}, -1, "",
     "inidica el frame de inicio"},
    {{"endframe", 1, NULL, OPT_VAL_IS_INT}, -1, "",
     "inidica el frame de fin"},
    {{"h", 0, NULL, true}, false, "", 
     "Muestra las opciones que se pueden indicar por línea de comandos"},
    {{"help", 0, NULL, true}, false, "", 
     "Muestra esta ventana de ayuda"},
    {{NULL, 0, NULL, 0}, 0, "", 
     ""}
};
